#!/bin/sh
#=======================================================================
# run_dyncore_test
#  Run the Jablonowski&Williamson (2006) dynamical core test
#                                            2012/11/03 Daisuke Hotta
#=======================================================================
set -e
compiler=pgi    # pgi or gfortran
testname=steady # steady or barocl
schemename='ncyc3' # name of the time-integration scheme
# version of N-cycle, choose one from the following:
#    _3a, _3b, _3ab, _4a, _4b, _4abba
# for Leapfrog, set this to '' (null string)
ncycversion='_4abba' 
cd ../../../
SPEEDY=`pwd`
# directory for output data
OUTPUT=${SPEEDY}/DATA/dyncore_${testname}_${schemename}${ncycversion}
# work directory
TMPDIR=${SPEEDY}/model/dyncore_test/wk_exec/${testname}_${schemename}${ncycversion}
#-----------------------------------------------------------------------
# Usually do not modify below
#-----------------------------------------------------------------------
mkdir -p $OUTPUT
sed -e 's/1982/1981/g' < $SPEEDY/common/yyyymmddhh.ctl > $OUTPUT/yyyymmddhh.ctl
sed -e 's/1982/1981/g' < $SPEEDY/common/yyyymmddhh_p.ctl > $OUTPUT/yyyymmddhh_p.ctl
#
# Work directory
#
rm -rf $TMPDIR
mkdir -p $TMPDIR
cd $TMPDIR
#
# 30-day run
#
IYYYY=1981
IMM=01
IDD=01
IHH=00
### build
echo '>>>BEGIN BUILDING SPEEDY MODEL'
cp -f $SPEEDY/model/source/makefile .
cp -f $SPEEDY/model/source/*.h .
cp -f $SPEEDY/model/source/*.f .
cp -f $SPEEDY/model/source/*.s .

mv par_horres_t30.h atparam.h
mv par_verres.h atparam1.h

cp -f $SPEEDY/model/ver32.input/cls_*.h .
cp -f $SPEEDY/model/ver32.input/inpfiles.s .

cp -f $SPEEDY/model/update/*.h .
cp -f $SPEEDY/model/update/*.f .
cp -f $SPEEDY/model/update/makefile .

## scheme specific
cp -f $SPEEDY/model/update2/${schemename}/*.f . || true
cp -f $SPEEDY/model/update2/${schemename}/*.h . || true
cp -f $SPEEDY/model/update2/${schemename}/cls_instep.h_1month ./cls_instep.h
if [[ ${schemename} == *ncyc* ]]; then 
  cp -f $SPEEDY/model/update2/ncyc_parm/ncycparm.h${ncycversion} ./ncycparm.h
fi
cp -f $SPEEDY/model/update2/${schemename}/makefile_${compiler} ./makefile

### dynamical core test
# switch-off physics
cp -f $SPEEDY/model/dyncore_test/source/phy_phypar.f .
# boundary condition
cp -f $SPEEDY/model/dyncore_test/data/orog_lsm_alb.t30.grd fort.20
# initial condition
cp -f $SPEEDY/model/dyncore_test/data/ini_${testname}.t30.grd ./fort.90

make imp.exe

sed -e 's/=../=..\/..\/../g' -e '/orog/d' <inpfiles.s>inpfiles2.s
sh inpfiles2.s t30

echo '>>>END BUILDING SPEEDY MODEL'
### run
FORT2=1111
echo ">>>BEGIN 30-DAY COMPUTATION OF $IYYYY/$IMM/$IDD/$IHH"
echo $FORT2 > fort.2
echo $IYYYY >> fort.2
echo $IMM >> fort.2
echo $IDD >> fort.2
echo $IHH >> fort.2
time ./imp.exe > out.lis1
mv ${IYYYY}*.grd ${OUTPUT}

echo "NORMAL END"
# rm ${TMPDIR}
